﻿using System.Collections.Generic;
using System.Linq;
using YuFeng.Models.User;
using YuFeng.Repositories;
using YuFeng.Repositories.User;

namespace YuFeng.Services.User
{
    public interface IUserService : IBaseService<Models.User.User>
    {
        Models.User.User GetUserByName(string name);
        IList<UserRole> GetUserRoles(string userid);

        Models.User.User UserLogin(string name, string password);

        Models.User.User GetUserById(string id);

    }
    public class UserService : BaseService<Models.User.User>, IUserService
    {
        private readonly IUserRepository _userRepository;
        private readonly IUserRoleRepository _userRoleRepository;
        private readonly IUnitOfWork _unitOfWork;
        public UserService(IUserRepository userRepository, IUnitOfWork unitOfWork, IUserRoleRepository userRoleRepository)
            : base(userRepository, unitOfWork)
        {
            this._userRepository = userRepository;
            this._unitOfWork = unitOfWork;
            _userRoleRepository = userRoleRepository;
        }
        public Models.User.User GetUserByName(string name)
        {
            return _userRepository.Get(o => o.Name == name);
        }
        public IList<UserRole> GetUserRoles(string userid)
        {
            return _userRoleRepository.GetMany(o => o.UserId == userid).ToList();
        }
        public Models.User.User UserLogin(string name, string password)
        {
            var query = _userRepository.Get(e => e.Name == name && e.Password == password);
            return query;
        }
        public Models.User.User GetUserById(string id)
        {
            var query = _userRepository.GetById(id);
            return query;
        }
    }
}
